1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include <sys/sem.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h>
union semun { int val; struct semid_ds* buf; unsigned short int* array; struct seminfo* __buf; };
void pv(int sem_id, int op) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = op; sem_b.sem_flg = SEM_UNDO; semop(sem_id, &sem_b, 1); }
int main(int argc, char* argv[]) { int sem_id = semget(IPC_PRIVATE, 1, 0666);
union semun sem_un; sem_un.val = 1; semctl(sem_id, 0, SETVAL, sem_un);
pid_t id = fork(); if (id < 0) { return 1; } else if (id == 0) { printf("child try to get binary sem\n"); pv(sem_id, -1); printf("child get the sem and would release it after 5 seconds\n"); sleep(5); pv(sem_id, 1); exit(0); } else { printf("parent try to get binary sem\n"); pv(sem_id, -1); printf("parent get the sem and would release it after 5 seconds\n"); sleep(5); pv(sem_id, 1); }
waitpid(id, NULL, 0); semctl(sem_id, 0, IPC_RMID, sem_un); return 0; }
|